package com.aifast.system.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.system.sys.entity.SysUser;
import com.aifast.system.sys.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aifast.common.base.Controller;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author fujian
 * @since 2022-01-07
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/sysUser")
public class SysUserController extends Controller {

    private final SysUserService service;

    @SaCheckPermission(value = "user:list")
    @GetMapping("list")
    public LayuiDataTable list() {
        return service.list(getSearchValue(), getInt("orgId"), getInt("state"),getInt("roleId"), getPage());
    }

    @AiIdempotent
    @SaCheckPermission("user:addOrUpdate")
    @PostMapping("add")
    public R add(SysUser sysUser) {
        return service.insertOrUpdate(sysUser,get("roleIds"),get("diyLabel")) ? OK() : ERROR();
    }

    @SaCheckPermission("user:list")
    @GetMapping("info")
    public R info() {
        return OK(service.info(getInt("id")));
    }

    @AiIdempotent
    @SaCheckPermission("user:del")
    @GetMapping("del")
    public R del() {
        return service.del(getInt("id"), get("ids")) ? OK() : ERROR();
    }

    @AiIdempotent
    @SaCheckPermission("user:resetPsw")
    @GetMapping("resetPsw")
    public R resetPsw(){
        return service.resetPsw(getInt("userId")) ? OK() : ERROR();
    }

    @AiIdempotent
    @SaCheckPermission("user:addOrUpdate")
    @GetMapping("updateUserState")
    public R updateUserState(){
        return service.updateUserState(getInt("userId"),getInt("state")) ? OK() : ERROR();
    }

    @SaCheckPermission("user:personal")
    @GetMapping("personal")
    public R personal(){
        return OK(service.personal());
    }

    @AiIdempotent
    @SaCheckPermission("user:updateByPersonal")
    @PostMapping("updateByPersonal")
    public R updateByPersonal(SysUser sysUser) {
        return service.insertOrUpdate(sysUser,null,get("diyLabel")) ? OK() : ERROR();
    }


    @SaCheckPermission("user:updateByPersonal")
    @PostMapping("uploadAvatar")
    public R uploadAvatar(){
        String img = service.updateAvatar(get("img"));
        return ObjectUtil.isNotNull(img) ? OK(img) : ERROR();
    }

    @SaCheckPermission("user:updateByPersonal")
    @PostMapping("updatePassword")
    public R updatePassword(){
        return service.updatePassword(get("password"),get("newPassword"),get("confirmNewPassword"));
    }

    @GetMapping("selectData")
    public R selectData(){
        return OK(service.selectData());
    }

}

